package cpa.prowon.gui;

import java.util.Properties;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pho.prowon.category.VisibleIndexCategory;
import cpa.prowon.Application;
import cpa.prowon.gui.util.ImageUtil;

/**
 * The rename category window is used to prompt for a new name for a category
 * @author Chris
 */
public class RenameCategoryWindow {

	// Properties
	protected MainWindow mainWindow;
	protected ResourceBundle rb;
	protected VisibleIndexCategory cat;
	protected Shell shell;
	
	/**
	 * Property name constant for the x value of the window
	 * @return The property name for the x value of the window
	 * @author Chris
	 */
	protected static final String PROPNAME_X = "prowon.RenameCategoryWindow.x";

	/**
	 * Property name constant for the y value of the window
	 * @return The property name for the y value of the window
	 * @author Chris
	 */
	protected static final String PROPNAME_Y = "prowon.RenameCategoryWindow.y";
	
	/**
	 * Configure the shell
	 * @author Chris
	 */
	protected void configureShell() {
		
		// Configure depending on the last remembered information
		Properties appProps = Application.getInstance().getApplicationProperties();
		
		// Set the location
		String x = appProps.getProperty(RenameCategoryWindow.PROPNAME_X, "");
		String y = appProps.getProperty(RenameCategoryWindow.PROPNAME_Y, "");
		if (x != "" & y != "") {
			int ix = Integer.parseInt(x);
			int iy = Integer.parseInt(y);
			shell.setLocation(ix, iy);
		}

	}

	/**
	 * Handle the situation when the window get closed
	 * In this case the window settings get saved
	 * @author Chris
	 */
	protected void cleanup() {
	
		// Save the window information
		Point location = shell.getLocation();
		String x = "" + location.x;
		String y = "" + location.y;
		Properties appProps = Application.getInstance().getApplicationProperties(); 
		appProps.setProperty(RenameCategoryWindow.PROPNAME_X, x);
		appProps.setProperty(RenameCategoryWindow.PROPNAME_Y, y);
	
	}

	/** Close the window
	 * @author Chris
	 */
	protected void close() {
		cleanup();
		shell.dispose();
	}

	/**
	 * Constructor for the RenameCategoryWindow
	 * @param MainWindow The main window which is the parent of the rename category window
	 * @param VisibleIndexCategory The index category which shall be renamed
	 * @author Chris
	 */
	public RenameCategoryWindow(MainWindow window, VisibleIndexCategory theCat) {
		
		// Set the main window
		mainWindow = window;
		
		// Set the category
		cat = theCat;
		
		// Get the resource bundle
		rb = Application.getInstance().getResourceBundle();
		
	}

	/**
	 * Open the window
	 * @author Chris
	 */
	public void open() {

		// Create the contents
		shell = createContents();
		
		// Configure the shell
		configureShell();
		
		// Open the shell
		shell.pack();
		shell.open ();

		// Dispose the shell
		Display display = shell.getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
	
	}
	
	/**
	 * Create the shell that holds all the window widgest
	 * @return The actual shell holding all widgets
	 * @author Chris
	 */
	protected Shell createShell() {

		// Create the shell
		Shell shell = new Shell(mainWindow.getShell(), SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
		GridLayout shellGrid = new GridLayout();
		shellGrid.marginWidth = 10;
		shellGrid.marginHeight = 5;
		shellGrid.numColumns = 2;
		shellGrid.makeColumnsEqualWidth = true;
		shell.setLayout(shellGrid);
		shell.setText (rb.getString("title_renamecategory"));
		shell.setImage(ImageUtil.getImage("icons/logo.gif"));

		// Return the shell
		return shell;
		
	}
	
	/**
	 * Create the contents of the window which is the widgest strucutre,
	 * all input fields, labels etc.
	 * @return The actual shell holding all widgets
	 * @author Chris
	 */
	protected Shell createContents() {

		// Create the shell
		final Shell shell = createShell();

		// Create the text field
		final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
		
		// Layout the text field
		GridData grid = new GridData();
		grid.grabExcessHorizontalSpace = true;
		grid.horizontalAlignment = GridData.FILL;
		grid.widthHint = 300;
		grid.horizontalSpan = 2;
		text.setLayoutData(grid);
		
		// Set the initial text
		text.setText(cat.getName());
		
		// Add a focus listener
		text.addFocusListener(
			new FocusListener() {
				public void focusGained(FocusEvent e) {
					((Text) e.widget).selectAll();
				}
				public void focusLost(FocusEvent e) {
				}
			}
		);
		
		// Create the OK button
		Button okButton = new Button(shell, SWT.PUSH);
		GridData okButtonGridData = new GridData();
		okButtonGridData.verticalSpan = 8;
		okButtonGridData.horizontalAlignment = GridData.END;
		okButtonGridData.widthHint = 100;
		okButton.setLayoutData(okButtonGridData);
		okButton.setText(Application.getInstance().getResourceBundle().getString("button_ok"));
		okButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				cat.rename(text.getText());
				close();
			}
		});
				
		// Make it the default button
		shell.setDefaultButton(okButton);
		
		// Create the Cancel button
		Button cancelButton = new Button(shell, SWT.PUSH);
		GridData cancelButtonGridData = new GridData();
		cancelButtonGridData.verticalSpan = 8;
		cancelButtonGridData.horizontalAlignment = GridData.BEGINNING;
		cancelButtonGridData.widthHint = 100;
		cancelButton.setLayoutData(cancelButtonGridData);
		cancelButton.setText(Application.getInstance().getResourceBundle().getString("button_cancel"));
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				close();
			}
		});
	
		return shell;
		
	}
	
}

